/**
 * All rights reserved by www.xmerge.cn
 */
package net.xfok.info.nba;

import java.util.Iterator;
import java.util.Map;

import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.res.Resources;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.util.Log;

/**
 * @author YangHua
 *
 */
public class InfoSettings extends PreferenceActivity implements OnSharedPreferenceChangeListener  {
	private static final String TAG="Settings";
	public static final String REFRESH_KEY="refresh";
	public static final String DELETE_KEY="delete";
	public static final String NOTIFY_KEY="CheckBoxNotify";
	public static final String DEFAULT_VALUE="1";
	private String[] refreshs;
	private String[] deletes;
	private Resources res;

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.settings);
        res=getResources();
        refreshs=res.getStringArray(R.array.refresh_items);
        deletes=res.getStringArray(R.array.delete_items);
        SharedPreferences sp=getPreferenceScreen().getSharedPreferences();
        sp.registerOnSharedPreferenceChangeListener(this);
        setValues(sp);
	}
	
	public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,String key) {
		// TODO Auto-generated method stub		
		setSummary(key, sharedPreferences);
	}
	
	private void setValues(SharedPreferences sharedPreferences){
		Map map=sharedPreferences.getAll();
		Iterator it=map.keySet().iterator();
		while(it.hasNext()){
			String key=it.next().toString();
			setSummary(key, sharedPreferences);
		}
	}
	
	private void setSummary(String key,SharedPreferences sharedPreferences){	
		Preference preference=(Preference)findPreference(key);
		if(key.equals(REFRESH_KEY)){		
			int value=Integer.parseInt(sharedPreferences.getString(REFRESH_KEY, DEFAULT_VALUE));
			Log.i(TAG, "value="+value);
			preference.setSummary(refreshs[value]);
		}else if(key.equals(DELETE_KEY)){
			int value=Integer.parseInt(sharedPreferences.getString(DELETE_KEY, DEFAULT_VALUE));
			Log.i(TAG, "value="+value);
			preference.setSummary(deletes[value]);
		}
	}
}
